package com.ww.springboot.mybatis.config;

import cn.hutool.core.util.ClassUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.UnknownTypeHandler;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

/**
 * todo
 *
 * @author wanwei
 * @since 2021-05-09 00:03
 */
@Configuration
//@Component
public class ApplicationReadyListener implements ApplicationListener<ApplicationReadyEvent> {

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        SqlSessionFactory sqlSessionFactory = event.getApplicationContext().getBean(SqlSessionFactory.class);
        ClassUtil.scanPackageByAnnotation("com.ww.springboot.mybatis", TableName.class).forEach(clazz -> {
            for (Field filed : clazz.getDeclaredFields()) {
                TableField tableName = filed.getAnnotation(TableField.class);
                if (null != tableName && !tableName.typeHandler().equals(UnknownTypeHandler.class)) {
                    //sqlSessionFactory.getConfiguration().getTypeHandlerRegistry().register(filed.getType(), tableName.typeHandler());
                }
            }
        });
    }
}
